﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

using AtletIS.Shared.Entities;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;

namespace AtletIS.Shared.Helpers
{
    public static class RefereeListHelper
    {

        #region Public methods

        /// <summary>
        ///  Vygeneruje zápis pre rozhodcov
        /// </summary>
        public static void GetRefereeList(DisciplineListEntity discipline)
        {
            string htmlFilePath = HtmlHelper.GetNewHtmlFileName("(Z) " + discipline.Name);
            StreamWriter sw = new StreamWriter(htmlFilePath, false, Encoding.Default);

            try
            {
                HtmlHelper.PrintHtmlHead(sw, "AtletIS");
                
                sw.WriteLine("<body>");

                HtmlHelper.PrintHeader(sw, "Zápis pre rozhodcov", Globals.ActiveCompetition);

                #region Data

                DataTable dtPerformances = DatabaseHelper.GetPerformancesByDisciplineListId(discipline.ID);
                if (dtPerformances != null && dtPerformances.Rows.Count > 0)
                {
                    // Hlavna disciplina
                    RefereeListHelper.GetRefereeListTable(discipline, sw);
                }
                else
                { 
                    // Rozdelena disciplina
                    string parentName = string.Empty;
                    DataTable dtParentDiscipline = DatabaseHelper.GetDisciplineListById(discipline.ParentId);
                    if (dtParentDiscipline != null && dtParentDiscipline.Rows.Count > 0)
                        parentName = dtParentDiscipline.Rows[0]["Name"].ToString();

                    DataTable dtDisciplines = DatabaseHelper.GetDisciplineListByParentIdActive(discipline.ID);
                    if (dtDisciplines != null && dtDisciplines.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtDisciplines.Rows)
                        {
                            DisciplineListEntity d = DisciplineListHelper.ConvertToEntity(dr);
                            d.Name = string.Format("{0} >> {1}", parentName, d.Name);
                            RefereeListHelper.GetRefereeListTable(d, sw);
                        }
                    }
                }

                #endregion

                HtmlHelper.PrintFooter(sw);

                sw.WriteLine("</body>");
                sw.WriteLine("</html>");

                sw.Close();
                sw.Dispose();

                WordHelper.CreateWordFromHtml(htmlFilePath);
            }
            catch (Exception ex)
            {
                if (sw != null)
                {
                    sw.Close();
                    sw.Dispose();
                }

                ExceptionHelper.ShowErrorInfo(ex);
            }
        }

        #endregion

        #region Private methods

        private static void GetRefereeListTable(DisciplineListEntity discipline, StreamWriter sw)
        {
            switch (discipline.DisciplineCode)
            {
                case Globals.SprintCode:
                case Globals.LongRunCode:
                case Globals.MarathonCode:
                    RefereeListHelper.RunTable(discipline, sw);
                    break;

                case Globals.ThrowsCode:
                case Globals.HorJumpCode:
                    RefereeListHelper.SixAttemptsTable(discipline, sw);
                    break;

                case Globals.VerJumpCode:
                    RefereeListHelper.XAttemptsTable(discipline, sw);
                    break;
            }
        }

        private static void GetLinesTable(StreamWriter sw)
        {
            sw.WriteLine();
            sw.WriteLine("<table class=\"extra\">");
            sw.WriteLine("<tr><td width=\"100%\"></td></tr>");
            sw.WriteLine("<tr><td width=\"100%\">Hl.rozhodca</td></tr>");
            sw.WriteLine("<tr><td width=\"100%\"></td></tr>");
            sw.WriteLine("<tr><td width=\"100%\">Rozhodcovia</td></tr>");
            sw.WriteLine("<tr><td width=\"100%\"></td></tr>");
            sw.WriteLine("<tr><td width=\"100%\">Zapisovateľ</td></tr>");
            sw.WriteLine("<tr><td width=\"100%\"></td></tr>");
            sw.WriteLine("</table>");
        }

        /// <summary>
        ///  Vygeneruje tabuľku zápisu pre 6-pokusovú disciplínu
        /// </summary>
        private static void SixAttemptsTable(DisciplineListEntity discipline, StreamWriter sw)
        {
            // Get performances
            DataTable dtPerformances = DatabaseHelper.GetPerformancesByDisciplineListId(discipline.ID);
            if (dtPerformances != null && dtPerformances.Rows.Count > 0)
            {
                sw.WriteLine("<h3>" + discipline.Name + "</h3>");
                sw.WriteLine("<table class=\"bordered-table\">");

                #region Print header

                sw.WriteLine("<tr height=\"30\" bgcolor=\"#D8D8D8\">");
                sw.WriteLine("<th width=\"30\">Št.č.</th>");
                sw.WriteLine("<th>Meno</th>");

                sw.WriteLine("<th width=\"50\">1</th>");
                sw.WriteLine("<th width=\"50\">2</th>");
                sw.WriteLine("<th width=\"50\">3</th>");

                sw.WriteLine("<th width=\"50\">Fin.</th>");

                sw.WriteLine("<th width=\"50\">4</th>");
                sw.WriteLine("<th width=\"50\">5</th>");
                sw.WriteLine("<th width=\"50\">6</th>");

                sw.WriteLine("<th width=\"60\">Výkon</th>");
                sw.WriteLine("<th width=\"50\">Por.</th>");

                sw.WriteLine("</tr>");

                #endregion

                int count = 1;
                foreach (DataRow dr in dtPerformances.Select(null, "Lane ASC, SB ASC, PB ASC, StartNumber, Surname"))
                {
                    #region Print row

                    PrintHelper.PrintRowHeader(sw, count);
                    sw.WriteLine("  <td align=\"center\">" + PrintHelper.PrintStartNumber(dr) + "</td>");

                    DateTime birthDate = Convert.ToDateTime(dr["BirthDate"]);
                    sw.WriteLine("  <td>" + PrintHelper.PrintName(dr) + "<br>");
                    sw.WriteLine(birthDate.Year.ToString() + " &bull; " + dr["Club"].ToString() + "</td>");

                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");

                    sw.WriteLine("</tr>");
                    count += 1;

                    #endregion
                }

                sw.WriteLine("</table>");
                sw.WriteLine();

                RefereeListHelper.GetLinesTable(sw);
            }
        }

        /// <summary>
        ///  Vygeneruje tabuľku zápisu pre X-pokusovú disciplínu
        /// </summary>
        private static void XAttemptsTable(DisciplineListEntity discipline, StreamWriter sw)
        {
            int columns = 12;

            DataTable dtPerformances = DatabaseHelper.GetPerformancesByDisciplineListId(discipline.ID);
            if (dtPerformances != null && dtPerformances.Rows.Count > 0)
            {
                sw.WriteLine("<h3>" + discipline.Name + "</h3>");
                sw.WriteLine("<table class=\"bordered-table\">");

                #region Print header

                sw.WriteLine("<tr height=\"30\" bgcolor=\"#D8D8D8\">");
                sw.WriteLine("<th width=\"30\">Št.č.</th>");
                sw.WriteLine("<th>Meno</th>");

                for (int i = 0; i < columns; i++)
                    sw.WriteLine("<th width=\"30\"></th>");

                sw.WriteLine("<th width=\"60\">Výkon</th>");
                sw.WriteLine("<th width=\"30\">Por.</th>");

                sw.WriteLine("</tr>");

                #endregion

                int count = 1;
                foreach (DataRow dr in dtPerformances.Select(null, "Lane ASC, SB ASC, PB ASC, StartNumber, Surname"))
                {
                    #region Print row

                    PrintHelper.PrintRowHeader(sw, count);
                    sw.WriteLine("  <td align=\"center\">" + PrintHelper.PrintStartNumber(dr) + "</td>");

                    DateTime birthDate = Convert.ToDateTime(dr["BirthDate"]);
                    sw.WriteLine("  <td>" + PrintHelper.PrintName(dr) + "<br>");
                    sw.WriteLine(birthDate.Year.ToString() + " &bull; " + dr["Club"].ToString() + "</td>");

                    for (int i = 0; i < columns; i++)
                        sw.WriteLine("<th width=\"20\"></th>");

                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");

                    sw.WriteLine("</tr>");
                    count += 1;

                    #endregion
                }

                sw.WriteLine("</table>");
                sw.WriteLine();

                RefereeListHelper.GetLinesTable(sw);
            }
        }

        /// <summary>
        ///  Vygeneruje tabuľku zápisu pre behy
        /// </summary>
        private static void RunTable(DisciplineListEntity discipline, StreamWriter sw)
        {
            // Get performances
            DataTable dtPerformances = DatabaseHelper.GetPerformancesByDisciplineListId(discipline.ID);
            if (dtPerformances != null && dtPerformances.Rows.Count > 0)
            {
                #region Sort

                string sSort = string.Empty;

                switch (discipline.DisciplineCode)
                {
                    case Globals.SprintCode:
                    case Globals.LongRunCode:
                        sSort = "Lane, SB DESC, PB DESC, StartNumber, Surname";
                        break;

                    case Globals.MarathonCode:
                        sSort = "StartNumber, Surname";
                        break;
                }

                #endregion

                sw.WriteLine("<h3>" + discipline.Name + "</h3>");
                sw.WriteLine("<table class=\"bordered-table\">");

                #region Print header

                sw.WriteLine("<tr height=\"30\" bgcolor=\"#D8D8D8\">");
                sw.WriteLine("<th width=\"30\">#</th>");
                sw.WriteLine("<th width=\"30\">Št.č.</th>");
                sw.WriteLine("<th>Meno</th>");

                sw.WriteLine("<th width=\"100\">Výkon</th>");
                sw.WriteLine("<th width=\"50\">Por.</th>");
                sw.WriteLine("<th width=\"150\">Poznámka</th>");

                sw.WriteLine("</tr>");

                #endregion

                int count = 1;
                foreach (DataRow dr in dtPerformances.Select(null, sSort))
                {
                    #region Print row

                    PrintHelper.PrintRowHeader(sw, count);

                    if (dr["Lane"].ToString() == "0")
                        sw.WriteLine("  <td align=\"center\"></td>");
                    else
                        sw.WriteLine("  <td align=\"center\">" + dr["Lane"].ToString() + "</td>");

                    sw.WriteLine("  <td align=\"center\">" + PrintHelper.PrintStartNumber(dr) + "</td>");

                    DateTime birthDate = Convert.ToDateTime(dr["BirthDate"]);
                    sw.WriteLine("  <td>" + PrintHelper.PrintName(dr) + "<br>");
                    sw.WriteLine(birthDate.Year.ToString() + " &bull; " + dr["Club"].ToString() + "</td>");

                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");
                    sw.WriteLine("<td></td>");

                    sw.WriteLine("</tr>");
                    count += 1;

                    #endregion
                }

                sw.WriteLine("</table>");
                sw.WriteLine();

                RefereeListHelper.GetLinesTable(sw);
            }
        }

        #endregion

    }
}
